package com.diy.sigmund.diycommon.test.service.async;

import com.diy.sigmund.diycommon.entity.CustomUser;
import com.diy.sigmund.diycommon.entity.ResultMsg;
import com.diy.sigmund.diycommon.entity.po.user.UserAuth;
import com.diy.sigmund.diycommon.spring.ContextHolder;
import com.diy.sigmund.diycommon.util.json.JacksonUtil;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

/**
 * @author ylm-sigmund
 * @since 2021/9/10 10:35
 */
@Service
public class AsyncTaskTestService {

    private static final Logger log = LoggerFactory.getLogger(AsyncTaskTestService.class);

    @Async("customAsyncExecutor")
    public void testPrint(UserAuth userAuth) throws Exception {
        log.info("这是测试ThreadPoolConfiguration");
        log.info("这是测试ThreadPoolTaskExecutor");
        log.info("这是测试@Configuration");
        log.info("这是测试异步线程池名称");
        throw new ClassNotFoundException();
    }

    @Async("customAsyncExecutor")
    public Future<ResultMsg> testFuture(UserAuth userAuth) throws Exception {
        log.info("这是测试ThreadPoolConfiguration");
        log.info("这是测试ThreadPoolTaskExecutor");
        log.info("这是测试@Configuration");
        log.info("这是测试异步线程池名称");
        CustomUser customUser = ContextHolder.getInstance().getCustomUser();
        log.info("testFuture 获取上下文用户信息customUser={}", JacksonUtil.toJson(customUser));
        TimeUnit.SECONDS.sleep(2);
        // CompletableFuture.completedFuture(ResultMsg.success(userAuth));
        return new AsyncResult<>(ResultMsg.success(userAuth));
    }

    @Async("customAsyncExecutor")
    public void testAsyncContextHolder(UserAuth userAuth) throws Exception {
        log.info("这是测试ThreadPoolConfiguration");
        log.info("这是测试ThreadPoolTaskExecutor");
        log.info("这是测试@Configuration");
        log.info("这是测试异步线程池名称");
        CustomUser customUser = ContextHolder.getInstance().getCustomUser();
        log.info("testAsyncContextHolder 获取上下文用户信息customUser={}", JacksonUtil.toJson(customUser));
        throw new ClassNotFoundException();
    }

    /** CompletableFuture是对Feature的增强，Feature只能处理简单的异步任务，而CompletableFuture可以将多个异步任务进行复杂的组合。 */
    @Async("customAsyncExecutor")
    public CompletableFuture<String> doSomething1(String message) throws InterruptedException {
        log.info("do something1: {}", message);
        TimeUnit.SECONDS.sleep(3);
        return CompletableFuture.completedFuture("do something1: " + message);
    }

    @Async("customAsyncExecutor")
    public CompletableFuture<String> doSomething2(String message) throws InterruptedException {
        log.info("do something2: {}", message);
        TimeUnit.SECONDS.sleep(5);
        return CompletableFuture.completedFuture("; do something2: " + message);
    }

    @Async("customAsyncExecutor")
    public CompletableFuture<String> doSomething3(String message) throws InterruptedException {
        log.info("do something3: {}", message);
        TimeUnit.SECONDS.sleep(7);
        return CompletableFuture.completedFuture("; do something3: " + message);
    }

}
